package com.ruoyi.system.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.system.domain.TbRoomReservation;
import com.ruoyi.system.service.ITbRoomReservationService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 预约房源Controller
 * 
 * @author ruoyi
 * @date 2021-06-21
 */
@RestController
@RequestMapping("/subyue")
public class TbRoomReservationController extends BaseController
{
    @Autowired
    private ITbRoomReservationService tbRoomReservationService;

    @PreAuthorize(hasPermi = "resource:subyue:addyuyue")
    @PostMapping("addyuyue")
    public Object addyuyue(@RequestBody TbRoomReservation roomReservation){
        boolean aa=false;

        System.out.println(roomReservation);

        roomReservation.setShowingsSex(1);
        roomReservation.setUserId(new Long(1));
        roomReservation.setStatus(0);
        //雪花算法id
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        long l = snowflake.nextId();
        roomReservation.setId(String.valueOf(l));

        aa=tbRoomReservationService.addyuyue(roomReservation);

        if(aa) {
            System.out.println(aa);
//            new Thread(new Runnable() {
//                @SneakyThrows
//                @Override
//                public void run() {
//                    String reserveTime = roomReservation.getReserveTime();
//                    System.out.println(reserveTime+"+++++++++++++===============");
//                    System.out.println(new DateTime()+"--------------------------");
//                    SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
//                    Date parse = sim.parse(roomReservation.getReserveTime());
//                    long between = DateUtil.between(new DateTime(), parse, DateUnit.MS);
//                    //时区原因，减掉8小时
//                    between=between-28800000;
//                    System.out.println(between);
//                    if(between>43200000){
//                        between=between-3600000;
//                    }
//                    rabMq.setMs(between);
//                    System.out.println(rabMq.getMs()+"修改之后++++++++++++++++++++++");
//                    rabbitTemplate.convertAndSend("fanoutExchange", "", "你好，我是消息");
//                }
//            }).start();
            return aa;
        }
        return aa;
    }


    /**
     * 查询预约房源列表
     */
    @PreAuthorize(hasPermi = "resource:subyue:list")
    @GetMapping("/list")
    public TableDataInfo list(TbRoomReservation tbRoomReservation)
    {
        startPage();
        List<TbRoomReservation> list = tbRoomReservationService.selectTbRoomReservationList(tbRoomReservation);
        for (TbRoomReservation roomReservation : list) {
            System.out.println(roomReservation.getRoom_name());
        }
        return getDataTable(list);
    }

    /**
     * 导出预约房源列表
     */
    @PreAuthorize(hasPermi = "resource:subyue:export")
    @Log(title = "预约房源", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbRoomReservation tbRoomReservation) throws IOException
    {
        List<TbRoomReservation> list = tbRoomReservationService.selectTbRoomReservationList(tbRoomReservation);
        ExcelUtil<TbRoomReservation> util = new ExcelUtil<TbRoomReservation>(TbRoomReservation.class);
        util.exportExcel(response, list, "预约房源数据");
    }

    /**
     * 获取预约房源详细信息
     */
    @PreAuthorize(hasPermi = "resource:subyue:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(tbRoomReservationService.selectTbRoomReservationById(id));
    }

    /**
     * 新增预约房源
     */
    @PreAuthorize(hasPermi = "resource:subyue:add")
    @Log(title = "预约房源", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbRoomReservation tbRoomReservation)
    {
        return toAjax(tbRoomReservationService.insertTbRoomReservation(tbRoomReservation));
    }

    /**
     * 修改预约房源
     */
    @PreAuthorize(hasPermi = "resource:subyue:edit")
    @Log(title = "预约房源", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbRoomReservation tbRoomReservation)
    {
        return toAjax(tbRoomReservationService.updateTbRoomReservation(tbRoomReservation));
    }

    /**
     * 删除预约房源
     */
    @PreAuthorize(hasPermi = "resource:subyue:remove")
    @Log(title = "预约房源", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(tbRoomReservationService.deleteTbRoomReservationByIds(ids));
    }
}
